package com.erp.module.yuliaofreight

import android.annotation.SuppressLint
import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.widget.EditText
import com.erp.CaptureActivity
import com.erp.R
import com.erp.bean.YuliaoGiFreightBean
import com.erp.mclass.QuanXian
import com.erp.module.yuliaofreight.YuliaoFreight.YuliaoCangweiActivity
import com.erp.module.yuliaofreight.inYuliaoGiFreight.inYuliaoGiFreightActivity
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.YuliaoGiFrightAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.permission.PermissionListener
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.*
import kotlinx.android.synthetic.main.fragmengt_yuliao_freight.*

class FragmentYuliaoGiFreight : BaseKFragment(),YuliaoFreightConteract.YuliaoGiFreightView, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener, View.OnClickListener {



    lateinit var yuliaolist: MutableList<YuliaoGiFreightBean>
    var cangweinames: MutableList<String> = ArrayList<String>()
    var cangweiids: MutableList<String> = ArrayList<String>()
    var updialog: AlertDialog?=null
    lateinit var P: IYuliaoGiFreightP
    lateinit var adapter: YuliaoGiFrightAdapter
    var scanCode = ""
    override fun getLayoutId(): Int {
        return R.layout.fragmengt_yuliao_freight
    }

    override fun initData() {
        val listsp = ArrayList<String>()
        listsp.add("")
        listsp.add("板材")
        listsp.add("封边条")
        listsp.add("贴纸")
        listsp.add("吸塑")
        listsp.add("型材")
        listsp.add("包装物")
        val sadapter = ArrayAdapter(context, R.layout.lv_sp_tvitem, listsp)
        SpLeibie.adapter = sadapter
        yuliaolist = ArrayList<YuliaoGiFreightBean>()
        P = IYuliaoGiFreightP(this, activity!!,yuliaolist)
        adapter = YuliaoGiFrightAdapter(yuliaolist, activity!!,this)
        YuliaoGoLv.adapter = adapter
        YuliaoFreightFragmentPv.setheader(true)
        YuliaoFreightFragmentPv.setOnHeaderRefreshListener(this)
        YuliaoFreightFragmentPv.setOnFooterRefreshListener (this)
        BtGuolv.setOnClickListener(this)
        BtInYuliao.setOnClickListener(this)
        BtCangwei.setOnClickListener(this)
        Btshuax.setOnClickListener(this)
        tvChongzhi.setOnClickListener(this)
        BtSRucang.setOnClickListener(this)
        BtSChucang.setOnClickListener(this)
        BtSQuliao.setOnClickListener(this)
        if(!QuanXian.isEditYuliaoFreightSpaceGi){
            BtInYuliao.visibility = View.GONE
        }
        if(!QuanXian.isEditYuliaoFreightSpace){
            BtCangwei.visibility = View.GONE
        }
        P.getCangweiData()
    }

    override fun outYuliaoChucang(yfsg_id: String) {
        updialog = Myutil.showupdatedialog(activity, "正在出仓", false)
        P.outYuliao(yfsg_id,"")
    }

    override fun fetch_yuliao(yfsg_id:String,length:String) {
        updialog = Myutil.showupdatedialog(activity, "正在取料", false)
        P.fetchYuliao(yfsg_id,length.trim(),"")
    }

    @SuppressLint("SetTextI18n")
    override fun setTotal(total: String) {
        tvTotal.text = "总数$total"
    }

    override fun outYuliaoChucangOk(data: String) {
        Myutil.MyTost("出仓成功")
        P.xiala()
    }

    override fun outYuliaoChucangNo(error: String) {
    }

    override fun getCangwei(cangweiid: MutableList<String>, cangweiname: MutableList<String>) {
        updialog?.dismiss()
        this.cangweiids = cangweiid
        this.cangweinames = cangweiname
        val av = ArrayAdapter(activity, android.R.layout.simple_dropdown_item_1line, cangweinames)
        AtCangwei.setAdapter(av)
        AtCangwei.setOnClickListener { AtCangwei.showDropDown() }
    }

    override fun onClick(v: View?) {
        when(v){
            BtGuolv -> {
                var yfs_id = "";
                if (cangweinames.contains(AtCangwei.text.toString())) {
                    cangweinames.indices
                            .filter { cangweinames[it] == AtCangwei.text.toString() }
                            .forEach { yfs_id = cangweiids[it] }
                }
                updialog = Myutil.showupdatedialog(activity, "正在获取数据", false)
                P.sousuo(yfs_id,SpLeibie.selectedItem.toString(),etLength.text.toString(),etWidth.text.toString(),etThickness.text.toString(),etColor.text.toString(),etName.text.toString())
                P.xiala()
            }
            BtInYuliao -> {
                val intent = Intent()
                var a = activity
                if(a != null){
                    intent.setClass(a, inYuliaoGiFreightActivity::class.java)
                    intent.putExtra("rucangcode", "")
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    startActivityForResult(intent, 1)
                }

            }
            BtCangwei -> {
                val intent = Intent()
                var a = activity
                if(a != null) {
                    intent.setClass(a, YuliaoCangweiActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    startActivityForResult(intent,1)
                }

            }
            Btshuax -> {
                updialog = Myutil.showupdatedialog(activity, "正在获取仓位数据", false)
                P.getCangweiData()
            }
            tvChongzhi -> {
                AtCangwei.setText("")
                SpLeibie.setSelection(0)
            }
            BtSRucang -> {
                saoma(1)
            }
            BtSChucang -> {
                saoma(2)
            }
            BtSQuliao -> {
                saoma(3)
            }
        }
    }

    fun saoma(code:Int){
        PermissionUtils.issaomiao(activity!!, object : PermissionListener {
            override fun onSucceed(requestCode: Int, grantPermissions: List<String>) {
                val intent = Intent()
                intent.setClass(activity!!, CaptureActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                startActivityForResult(intent, code)
            }

            override fun onFailed(requestCode: Int, deniedPermissions: List<String>) {
                Myutil.MyTost("没有权限，无法扫描")
            }
        })
    }

    override fun xialaOk() {
        updialog?.dismiss()
        YuliaoFreightFragmentPv.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        YuliaoFreightFragmentPv.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        YuliaoFreightFragmentPv.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        YuliaoFreightFragmentPv.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when (requestCode) {
            1 -> if (resultCode == Activity.RESULT_OK) {
                val bundle = data.extras
                //显示扫描到的内容
                var s: String = bundle?.get("result") as String
                val intent = Intent()
                var a = activity
                if(a != null){
                    intent.setClass(a, inYuliaoGiFreightActivity::class.java)
                    intent.putExtra("rucangcode", s)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    startActivityForResult(intent, 1)
                }

            }
            2 -> if (resultCode == Activity.RESULT_OK) {
                val bundle = data.extras
                //显示扫描到的内容
                var s: String = bundle?.get("result") as String
                var a = activity
                if(a != null){
                    var dialog= AlertDialog.Builder(a)
                    dialog.setTitle("提示")
                    dialog.setMessage("确定出仓？")
                    dialog.setPositiveButton("确定") { dialog, _ ->
                        updialog = Myutil.showupdatedialog(activity, "正在出仓", false)
                        P.outYuliao("",s)
                    }
                    dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                    dialog.show()
                }

            }
            3 -> if (resultCode == Activity.RESULT_OK) {
                val bundle = data.extras
                //显示扫描到的内容
                var scan: String = bundle?.get("result") as String
                var a = activity
                if(a != null){
                    val dialog = AlertDialog.Builder(a).create()
                    dialog.setView(EditText(activity))
                    dialog.show()
                    dialog.setContentView(R.layout.dialog_etyuliaocangwei)
                    dialog.CangweiEt.setLines(20)
                    val window = dialog.window
                    dialog.title.text = "取料"
                    val p = window?.attributes // 获取对话框当前的参数值
                    p?.width = (sp.getInt("width", 0) * 0.9).toInt() // 宽度设置为屏幕的0.65
                    window?.attributes = p
                    dialog.LeftBt.setOnClickListener {
                        var s=dialog.CangweiEt.text.toString()
                        if(s.trim()==""){
                            Myutil.MyTost("必须输入长度才能确定")
                        }else {
                            updialog = Myutil.showupdatedialog(activity, "正在取料", false)
                            P.fetchYuliao("",dialog.CangweiEt.text.toString().trim(),scan)
                            dialog.dismiss()
                        }
                    }
                    dialog.RightBt.setOnClickListener {
                        dialog.dismiss()
                    }
                }

            }
        }
    }
}